<?php

class Bmap{

	public function getBMAPConverXy($Latitude, $Longitude){
		global $app_config;
		$baiduxyurl = "http://api.map.baidu.com/geoconv/v1/?coords={$Longitude},{$Latitude}&from=3&to=5&ak=$app_config[key_baidumap]";
		$result = json_decode(file_get_contents($baiduxyurl));
		$rs = array();
		$rs["Longitude"] = $result->result[0]->x;
		$rs["Latitude"] = $result->result[0]->y;
		return $rs;
	}
	
	public function getBMAPfromxy($Latitude, $Longitude, $zoom=16){
		global $app_config;
		$baiduxyurl = "http://api.map.baidu.com/geoconv/v1/?coords={$Longitude},{$Latitude}&from=3&to=5&ak=$app_config[key_baidumap]";
		import("Common.php");
		$result = json_decode(Common::http_request($baiduxyurl));
		$rs = array();
		$rs["Longitude"] = $result->result[0]->x;
		$rs["Latitude"] = $result->result[0]->y;
		$rs["picurl"] = "http://api.map.baidu.com/staticimage?width=540&height=300&center=".$rs["Longitude"].",".$rs["Latitude"]."&zoom=$zoom&markers=".$rs["Longitude"].",".$rs["Latitude"]."&markerStyles=1";
		return $rs;
	}
	
	/*
	 * $origin = latitude,longitude
	 * $dest = latitude,longitude
	 */
	public function getBMAPline($origin, $dest, $mode="driving"){
		global $app_config;
		$ocity_rs = Bmap::getBMAPCITY($origin);
		$ocity = $ocity_rs->result->addressComponent->city;
		$dcity_rs = Bmap::getBMAPCITY($origin);
		$dcity = $dcity_rs->result->addressComponent->city;
		//$ocity = "天津";
		//$dcity = "天津";
		$lineurl = "http://api.map.baidu.com/direction/v1?mode=$mode&origin=$origin&destination=$dest&origin_region=$ocity&destination_region=$dcity&output=json&ak=$app_config[key_baidumap]";
		import("Common.php");
		$result = json_decode(Common::http_request($lineurl));
		return $result;
	}
	
	/*
	 * $position = latitude,longitude
	*/
	public function getBMAPCITY($position){
		global $app_config;
		$cityurl = "http://api.map.baidu.com/geocoder/v2/?callback=&location=$position&output=json&pois=1&ak=$app_config[key_baidumap]";
		//http://api.map.baidu.com/geocoder/v2/?callback=&location=39.136300,117.189000&output=json&pois=1&ak=SSnAibQyDvZU09cFUtZwhY7n
		import("Common.php");
		$result = json_decode(Common::http_request($cityurl));
		return $result;
	}
	
	public function getBMAPForName($address, $city="天津", $insub=0){
		global $app_config;
		//if(!$city) $city="全国";
		$baiduxyurl = "http://api.map.baidu.com/geocoder/v2/?ak=$app_config[key_baidumap]&callback=&output=json&address=$address&city=$city";
		//echo $baiduxyurl;
		import("Common.php");
		$result = json_decode(Common::http_request($baiduxyurl));
		if($result->status){
			if(!$insub && $city != "全国"){
				Bmap::getBMAPForName($address, "全国", 1);
			}else{
				throw new Exception("查询失败, 错误代码:".$result->status);
			}
		}
		$rs = $result->result->location;
		return $rs;
	}
	
	public function getBMAPForPlace($place, $city="天津", $insub=0){
		global $app_config;
		//if(!$city) $city="天津";
		$baiduxyurl = "http://api.map.baidu.com/place/v2/search?ak=$app_config[key_baidumap]&output=json&query=$place&page_size=1&page_num=0&scope=1&region=$city";
		//echo $baiduxyurl;
		import("Common.php");
		$result = json_decode(Common::http_request($baiduxyurl));
		if($result->status){
			if(!$insub && $city != "全国"){
				//echo "a";
				Bmap::getBMAPForPlace($place, "天津", 1);
			}else{
				//echo "b";
				throw new Exception("查询失败, 错误代码:".$result->status);
			}
		}
		$rs = $result->results[0]->location;
		return $rs;
	}
	
	public function getBMAPlacelist($key, $region="天津", $insub=0){
		global $app_config;
		//if(!$region) $region="天津";
		$baiduxyurl = "http://api.map.baidu.com/place/v2/suggestion?query=$key&region=$region&output=json&ak=$app_config[key_baidumap]";
		//echo $baiduxyurl;
		import("Common.php");
		$result = json_decode(Common::http_request($baiduxyurl));
		if($result->status){
			if(!$insub && $region != "全国"){
				Bmap::getBMAPlacelist($key, "全国", 1);
			}else{
				throw new Exception("查询失败, 错误代码:".$result->status);
			}
		}
		$list_rs = array();
		foreach($result->result as $key=>$val){
			$list_rs[$key]["label"] = $val->name; 
			$list_rs[$key]["city"] = $val->city; 
			$list_rs[$key]["district"] = $val->district;
		}
		return $list_rs;
	}
}
?>